<template>
    <tnxel-submit-form ref="form" :model="model" :rules="rules">
        <el-form-item label="父工程">
            <MavenProjectPath :project="parent"/>
        </el-form-item>
        <el-form-item label="groupId" prop="groupId" class="new-error-line">
            <el-input v-model="model.groupId" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="artifactId" prop="artifactId" class="new-error-line">
            <el-input v-model="model.artifactId" placeholder="请输入"/>
        </el-form-item>
    </tnxel-submit-form>
</template>

<script>
import tnx from '@/tnx.js';

import MavenProjectPath from './components/MavenProjectPath.vue';

export default {
    components: {MavenProjectPath},
    props: {
        parent: {
            type: Object,
            required: true,
        },
        after: {
            type: Function,
            required: true,
        },
    },
    data() {
        let groupId = this.parent.groupId;
        if (this.parent.groupId !== this.parent.artifactId && !groupId.endsWith('.' + this.parent.artifactId)) {
            groupId += '.' + this.parent.artifactId;
        }
        return {
            model: {
                groupId: groupId,
                artifactId: '',
            },
            rules: {
                groupId: [tnx.app.validator.requiredRule],
                artifactId: [tnx.app.validator.requiredRule,
                    {
                        validator: (rule, value, callback) => {
                            if (this.parent.modules) {
                                let existedModule = this.parent.modules.find(m => m.artifactId === value);
                                if (existedModule && !existedModule.removed) {
                                    callback(new Error('已存在同名模块'));
                                }
                            }
                            callback();
                        },
                        trigger: 'blur',
                    }
                ],
            },
        };
    },
    methods: {
        dialog() {
            return {
                title: '添加简单模块',
                width: 400,
                type: 'confirm',
                click: this.toSubmit,
            };
        },
        toSubmit(yes, close) {
            if (yes) {
                this.$refs.form.toSubmit(() => {
                    this.after(this.model);
                    close();
                });
                return false;
            }
        },
    },
}
</script>

<style lang="scss" scoped>
</style>
